/**
 *  JSimProd - Java simulation code for production processes
 *  Copyleft Jorge Mira Yagüe <jorge.mira.yague@gmail.com>
 *  Universidad de Alicante 2009
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package gui.tools.models;

import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.ListModel;

/**
 * This class is used as a model to show strings in a JList
 * It allows to add and remove strings from the model, updating the view of the JList
 * @see JList
 * @see ListModel
 * @see DefaultListModel
 * @author Jorge Mira Yagüe
 */
public class LMString extends DefaultListModel {

    private List<String> strings;

    /**
     * Class builder
     * @param strings  The string list to show in the list viewer
     */
    public LMString(List<String> strings) {
        super();

        setStrings(strings);
    }

    public List<String> getStrings() {
        return strings;
    }

    public void setString(int index, String string) {
        strings.set(index, string);
        refresh();
    }

    public void addString(String string) {
        strings.add(string);
        refresh();
    }

    public String removeString(int index) {
        String result = strings.remove(index);
        refresh();
        return result;
    }

    public void removeString(String string) {
        strings.remove(string);
        refresh();
    }

    public String getString(int index) {
        return strings.get(index);
    }

    public void setStrings(List<String> strings) {
        this.strings = strings;
        refresh();
    }

    private void refresh() {
        removeAllElements();
        for (String s : strings) {
            addElement(s);
        }
    }
}
